package com.hzw.saas.web.hbox.orika;

import java.util.List;

import com.hzw.saas.api.storage.bo.ResourceFileBO;
import com.hzw.saas.api.storage.bo.ResourceFolderStructBO;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.web.hbox.constants.HBoxConsts;
import com.hzw.saas.web.hbox.dto.HBoxFileDTO;
import com.hzw.saas.web.hbox.dto.HBoxFolderDTO;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;

/**
 * @author EZ09
 * @since 07/27/2021
 */
public class HBoxFolderConverter extends BidirectionalConverter<ResourceFolderStructBO, HBoxFolderDTO> {

    @Override
    public HBoxFolderDTO convertTo(ResourceFolderStructBO source, Type<HBoxFolderDTO> destinationType, MappingContext mappingContext) {
        HBoxFolderDTO result = new HBoxFolderDTO();
        result.setFolderId(source.getPid())
            .setParentId(source.getSupId())
            .setName(source.getName())
            .setStatus(source.getStatus())
            .setCreatorId(source.getCreatorId())
            .setOwnerId(source.getCreatorId())
            .setCreatorName(source.getCreatorName())
            .setOwnerName(source.getCreatorName())
            .setCreateDate(source.getCreateTime())
            .setUpdateDate(source.getUpdateTime());
        List<HBoxFileDTO> subFiles = MapperUtil.nf().mapAsList(source.getSubFiles(), HBoxFileDTO.class);
        List<HBoxFolderDTO> subFolders = MapperUtil.nf().mapAsList(source.getSubFolders(), HBoxFolderDTO.class);
        result.setSubFiles(subFiles);
        result.setSubFolders(subFolders);
        return result;
    }

    @Override
    public ResourceFolderStructBO convertFrom(HBoxFolderDTO source, Type<ResourceFolderStructBO> destinationType, MappingContext mappingContext) {
        ResourceFolderStructBO result = new ResourceFolderStructBO();
        result.setPid(source.getFolderId())
            .setCreatorId(source.getCreatorId())
            .setEditorId(source.getCreatorId())
            .setCreateTime(source.getCreateDate())
            .setUpdateTime(source.getUpdateDate());
        result.setSpaceId(HBoxConsts.SPACE_ID)
            .setSupId(source.getParentId())
            .setName(source.getName())
            .setStatus(source.getStatus())
            .setCreatorName(source.getCreatorName())
            .setEditorName(source.getCreatorName());
        List<ResourceFileBO> subFiles = MapperUtil.nf().mapAsList(source.getSubFiles(), ResourceFileBO.class);
        List<ResourceFolderStructBO> subFolders = MapperUtil.nf().mapAsList(source.getSubFolders(), ResourceFolderStructBO.class);
        result.setSubFiles(subFiles);
        result.setSubFolders(subFolders);
        return result;
    }
}
